const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nyinit: 0,
    sort: 0,
    istype: 1,
    id: '',
    dateNo: "",
    orderData: [],
    territoryList: [],
    salesPersonList: [],
    queryform: {
      territory: '',
      duty: ''
    }
  },
  territoryHeld(e) {
    console.log(e);
    const id = e.currentTarget.dataset.id
    this.selectComponent('#' + id).show()
  },
  bindChangeterritoryjd(e) {
    this.setData({
      'queryform.territory': e.detail.label,
      'queryform.province': Number(e.detail.value),
    })
    this.getList()
    this.selectComponent('#territory').hideModal()
  },
  bindChangedutyjd(e) {
    this.setData({
      'queryform.duty': e.detail.label,
      'queryform.salesPerson': e.detail.value,
    })
    this.getList()
    console.log(e.detail.value);
    this.selectComponent('#duty').hideModal()
  },
  fromaTimes(date, formatNumber) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month);
    if (this.data.nyinit == 0) {
      return year + '-' + (month < 10 ? ('0' + month) : month)
    } else {
      return year + ''
    }
    // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  },
  sortHeld() {
    if (this.data.sort == 0) {
      this.setData({
        sort: 1,
      })
    } else {
      this.setData({
        sort: 0,
      })
    }
  },
  getListzrr() {
    let that = this
    that.setData({
      salesPersonList: []
    })
    wx.request({
      url: (wx.getStorageSync('tabNav') == 3 ? app.globalData.serverUrlgdb : app.globalData.serverUrl) + '/applet/get/person',
      method: 'get',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        let salesPersonList = []
        if (resp.code == 200) {
          if (resp.data.salesPersonList.length != 0) {
            resp.data.salesPersonList.map(item => {
              let obj = {}
              obj.label = item
              obj.value = item
              obj.checked = false
              salesPersonList.push(obj)
            })
            salesPersonList.unshift({
              value: '',
              label: '全部',
              checked: true
            })
            that.setData({
              salesPersonList
            })
          }
        }
      }
    })
  },
  getList() {
    let that = this
    let newData = {}
    wx.showLoading({
      title: '',
      mask: true
    })
    // newData.type = that.data.istype
    // if(that.data.nyinit==1){
    //   newData.year = that.data.dateNo
    // }else{
    //   newData.yearAndMonth = that.data.dateNo
    // }
    newData.province = that.data.queryform.province ? that.data.queryform.province : ''
    newData.salesPerson = that.data.queryform.salesPerson ? that.data.queryform.salesPerson : ''
    wx.request({
      url: app.globalData.serverUrlgdb + '/statistics/count/devThLifeExpire/rank',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: newData,
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          that.setData({
            orderData: resData.data.thLifeExpireRankList
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
        }
      })
    })
  },
  getdisList() {
    let that = this
    that.setData({
      territoryList: []
    })
    wx.request({
      url: app.globalData.serverUrlgdb + '/district/province/relate/list',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        if (resp.code == 200) {
          resp.data.provinceList.map(item => {
            item.value = item.id
            item.label = item.name
            item.checked = false
          })
          resp.data.provinceList.unshift({
            value: '0',
            label: '全部',
            checked: true
          })
          that.setData({
            territoryList: resp.data.provinceList
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.setData({
    //   dateNo: this.fromaTimes(new Date())
    // })
    this.getdisList()
    this.getListzrr()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      'queryform.territory':'',
      'queryform.province':'',
      'queryform.duty':'',
      'queryform.salesPerson':''
    })
    this.getdisList()
    this.getListzrr()
    this.getList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})